package taskhandler

import (
	"encoding/json"
	"fmt"

	"github.com/astaxie/beego/logs"

	"cvevulner/util"
)

type CommentParam struct {
	AccessToken string `json:"access_token"`
	Body        string `json:"body"`
}

// AddCommentToIssue Add a comment to the issue
func AddCommentToIssue(msg, issueNum, owner, repo, access string) {
	url := fmt.Sprintf(`https://gitee.com/api/v5/repos/%v/%v/issues/%v/comments`, owner, repo, issueNum)
	param := CommentParam{
		AccessToken: access,
		Body:        msg,
	}

	body, _ := json.Marshal(param)
	_, err := util.HTTPPost(url, string(body))
	if err != nil {
		logs.Error("add comment to issue num failed ", issueNum, err)
	}
}

// SendPrivateLetters Send a private message to a gitee user
func SendPrivateLetters(access, content, useName string) {
	url := "https://gitee.com/api/v5/notifications/messages"
	param := fmt.Sprintf(`{"access_token":"%s","username":"%s","content":"%s"}`, access, useName, content)
	res, err := util.HTTPPost(url, param)
	if err != nil {
		logs.Error(err)
	}
	logs.Info("Send private message:", res)
}
